package com.yanqu.road.dao.impl.activity.wishpool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.wishpool.WishPoolUserData;
import com.yanqu.road.entity.activity.wishpool.WishPoolUserReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * 许愿池用户数据dao
 */
public class WishPoolUserDataDaoImpl extends TempDataObjectDao<WishPoolUserData> {

    public static String getWishPoolUserDataSql = "select * from t_u_wish_pool where userId=? and activityId=?";

    public WishPoolUserData getWishPoolUserData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWishPoolUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWishPoolUserDataSql = "update t_u_wish_pool set wishValue=?, resetTime=?, wishTimes=?, wishRewards=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(WishPoolUserData wishPoolUserData) {
        wishPoolUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getWishValue()));
        para.put(i++, new DbParameter(Types.BIGINT, wishPoolUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getWishTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wishPoolUserData.getWishRewardList())));
        para.put(i++, new DbParameter(Types.BIGINT, wishPoolUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getActivityId()));
        if (getDb().executeNoneQuery(updateWishPoolUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWishPoolUserDataSql = "insert into t_u_wish_pool(userId,activityId,wishValue,resetTime,wishTimes,wishRewards) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(WishPoolUserData wishPoolUserData) {
        wishPoolUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, wishPoolUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getWishValue()));
        para.put(i++, new DbParameter(Types.BIGINT, wishPoolUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, wishPoolUserData.getWishTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(wishPoolUserData.getWishRewardList())));
        if (getDb().executeNoneQuery(insertWishPoolUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WishPoolUserData parseData(ResultSet rs) throws SQLException {
        WishPoolUserData wishPoolUserData = new WishPoolUserData();
        wishPoolUserData.setUserId(rs.getLong("userId"));
        wishPoolUserData.setActivityId(rs.getInt("activityId"));
        wishPoolUserData.setWishValue(rs.getInt("wishValue"));
        wishPoolUserData.setResetTime(rs.getLong("resetTime"));
        wishPoolUserData.setWishTimes(rs.getInt("wishTimes"));
        wishPoolUserData.setWishRewardList(JSONArray.parseArray(rs.getString("wishRewards"), WishPoolUserReward.class));
        return wishPoolUserData;
    }
}
